package com.puman.watchtrade.fragment.info;

import java.util.HashMap;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler.Callback;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.webkit.WebSettings.RenderPriority;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;
import android.widget.Toast;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;
import cn.sharesdk.onekeyshare.ShareContentCustomizeCallback;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.wechat.moments.WechatMoments;

import com.mob.tools.utils.UIHandler;
import com.puman.watchtrade.MainActivity;
import com.puman.watchtrade.R;

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@SuppressLint({ "ValidFragment", "NewApi" })
public class InfoDetailFragment extends Fragment implements OnClickListener,PlatformActionListener, Callback {
	private View mView;

	/*
	 * UI
	 */
	private WebView newsWebview;
	private TextView topTitle;
	private TextView shareLayout;

	/*
	 * variables
	 */
	private String title;
	private static final String APP_CACAHE_DIRNAME = "/webcache";
	private String infoUrl = "";
	private String imgUrl = "";
	private String flagString = "1";

	public InfoDetailFragment() {
	}

	public InfoDetailFragment(String Url,String imgUrl, String title) {
		if (title.length() > 6) {
			this.title = title.substring(0, 6);
		} else {
			this.title = title;
		}
		this.infoUrl = Url;
		this.imgUrl = imgUrl;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

	}

	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mView = inflater.inflate(R.layout.infodetail_layout, null);
		initView();
		return mView;
	}

	private void initView() {
		mView.findViewById(R.id.main_left_imgbtn).setOnClickListener(this);
		newsWebview = (WebView) mView.findViewById(R.id.share_webview);
		topTitle = (TextView) mView.findViewById(R.id.top_title);
		topTitle.setText(title);
		shareLayout = (TextView) mView.findViewById(R.id.share_layout);
		shareLayout.setOnClickListener(this);
		// newsWebview = new WebView(MainActivity.getInstant()
		// .getApplicationContext());
		// LinearLayout mll = (LinearLayout)
		// mView.findViewById(R.id.web_layout);
		// mll.addView(newsWebview);

		newsWebview.getSettings().setJavaScriptEnabled(true);
		newsWebview.getSettings().setUseWideViewPort(true);
		newsWebview.getSettings().setLoadWithOverviewMode(true);
		WebSettings webSettings = newsWebview.getSettings();
		webSettings.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);

		newsWebview.getSettings().setSupportZoom(true);
		newsWebview.getSettings().setBuiltInZoomControls(true);
		newsWebview.getSettings().setUseWideViewPort(true);
		newsWebview.getSettings().setAllowFileAccess(true);
		newsWebview.getSettings().setPluginsEnabled(true);

		newsWebview.getSettings().setRenderPriority(RenderPriority.HIGH);
		newsWebview.getSettings().setCacheMode(
				WebSettings.LOAD_CACHE_ELSE_NETWORK); // ���� ����ģʽ
		newsWebview.getSettings().setDomStorageEnabled(true);
		newsWebview.getSettings().setDatabaseEnabled(true);
		String cacheDirPath = MainActivity.getInstant().getFilesDir()
				.getAbsolutePath()
				+ APP_CACAHE_DIRNAME;
		newsWebview.getSettings().setDatabasePath(cacheDirPath);
		newsWebview.getSettings().setAppCachePath(cacheDirPath);
		newsWebview.getSettings().setAppCacheEnabled(true);

		// newsWebview.setBarHeight(3);
		newsWebview.setWebViewClient(new WebViewClient() {

			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				view.loadUrl(url);
				return true;
			}
		});
		newsWebview.setWebChromeClient(new WebChromeClient() {

			public void openFileChooser(ValueCallback<Uri> uploadMsg,
					String acceptType, String capture) {
				MainActivity.getInstant().mUploadMessage = uploadMsg;
				Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
				intent.addCategory(Intent.CATEGORY_OPENABLE);
				intent.setType("image/*");
				getActivity().startActivityForResult(
						Intent.createChooser(intent, ""),
						MainActivity.getInstant().FILECHOOSER_RESULTCODE);

			}

			// 3.0 + �����������
			public void openFileChooser(ValueCallback<Uri> uploadMsg,
					String acceptType) {
				MainActivity.getInstant().mUploadMessage = uploadMsg;
				Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
				intent.addCategory(Intent.CATEGORY_OPENABLE);
				intent.setType("image/*");
				getActivity().startActivityForResult(
						Intent.createChooser(intent, "��ɲ�����Ҫʹ��"),
						MainActivity.getInstant().FILECHOOSER_RESULTCODE);
			}

			// Android < 3.0 �����������
			public void openFileChooser(ValueCallback<Uri> uploadMsg) {
				MainActivity.getInstant().mUploadMessage = uploadMsg;
				Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
				intent.addCategory(Intent.CATEGORY_OPENABLE);
				intent.setType("image/*");
				getActivity().startActivityForResult(
						Intent.createChooser(intent, "��ɲ�����Ҫʹ��"),
						MainActivity.getInstant().FILECHOOSER_RESULTCODE);

			}

		});
		showWeb();
	}

	public void showWeb() {
		newsWebview.loadUrl(infoUrl);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.main_left_imgbtn:
			MainActivity.getInstant().fragmentManager.popBackStack();
			break;
		case R.id.share_layout:
//			MainActivity.getInstant().oks.setUrl(infoUrl);
//			ShareSDK.initSDK(MainActivity.getInstant());
//			MainActivity.getInstant().oks.setTitle(infoUrl);
//			MainActivity.getInstant().oks.show(getActivity());
			showShare();
			break;
		default:
			break;
		}
	}
	
	private void showShare() {
		 ShareSDK.initSDK(MainActivity.getInstant());
		 OnekeyShare oks = new OnekeyShare();
//			oks.disableSSOWhenAuthorize();
			oks.setTitle(getString(R.string.share));
			oks.setTitleUrl(infoUrl);
			oks.setText(title);
			oks.setUrl(infoUrl);
			oks.setComment("...");
			oks.setSite(getString(R.string.app_name));
			oks.setSiteUrl(infoUrl);
			oks.setCallback(this);
			//此处为本demo关键为一键分享折子定义分享回调函数   shareContentCustomuzeCallback
			//自定义平台可以通过判断不同的平台来实现不同平台间的不同操作
			oks.setShareContentCustomizeCallback(new ShareContentCustomizeCallback() {
				//自定义分享的回调想要函数
				@Override
				public void onShare(Platform platform,
						cn.sharesdk.framework.Platform.ShareParams paramsToShare) {
					//点击微信好友
					if("Wechat".equals(platform.getName())){
						paramsToShare.setTitle("会麦奢侈品金融");
						paramsToShare.setText(title);
						paramsToShare.setShareType(Platform.SHARE_WEBPAGE);
						paramsToShare.setImageUrl(imgUrl.replace(" ", ""));
						paramsToShare.setUrl(infoUrl);
					}
					if(WechatMoments.NAME.equals(platform.getName())){
						paramsToShare.setTitle("会麦奢侈品金融");
						paramsToShare.setText(title);
						paramsToShare.setShareType(Platform.SHARE_WEBPAGE);
						paramsToShare.setImageUrl(imgUrl.replace(" ", ""));
						paramsToShare.setUrl(infoUrl);
					}
					
					//点击新浪微博
					if("SinaWeibo".equals(platform.getName())){
						//限制微博分享的文字不能超过20
						if(paramsToShare.getText().length() > 20){
							Toast.makeText(MainActivity.getInstant(), "分享长度不能超过20个字", Toast.LENGTH_SHORT).show();
						}
						paramsToShare.setTitle("会麦奢侈品金融");
						paramsToShare.setText(title +" "+infoUrl);
						paramsToShare.setImageUrl(imgUrl.replace(" ", ""));
					}
					//点击除了QQ以外的平台
					if(!"QQ".equals(platform.getName())){
						Log.i("", "点击了QQ以外的平台");
					}
				}
			});
		// 启动分享GUI
		 oks.show(MainActivity.getInstant());
		 }
	@Override
	public void onCancel(Platform platf, int arg1) {
		Log.i("", "onCancel");
		if(arg1 == Platform.ACTION_SHARE){
			UIHandler.sendEmptyMessage(SHARE_CANCEL, this);
		}
	}

	@Override
	public void onComplete(Platform platf, int arg1,
			HashMap<String, Object> arg2) {
		Log.i("", "onComplete");
		if(arg1 == Platform.ACTION_SHARE){
			UIHandler.sendEmptyMessage(SHARE_SUCCESS, this);
			Log.i("", "响应分享事件");
		}
		if(arg1 == Platform.SHARE_TEXT){
			Log.i("", "响应分享文本事件");
			UIHandler.sendEmptyMessage(SHARE_SUCCESS, this);
			Log.i("", "..");
		}
	}

	@Override
	public void onError(Platform platf, int arg1, Throwable arg2) {
		// TODO Auto-generated method stub
		Log.i("", "onError");
		if(arg1 == Platform.ACTION_SHARE){
			UIHandler.sendEmptyMessage(SHARE_ERROR, this);
		}
	}
	private final  static int SHARE_SUCCESS = 1;
	private final  static int SHARE_CANCEL = 2;
	private final  static int SHARE_ERROR = 3;
	@Override
	public boolean handleMessage(Message msg) {
		Log.i("", "what = " + msg.what);
		switch(msg.what) {
		case SHARE_SUCCESS: {
			Log.i("", "arg1 = " + "分享成功");
//			Toast.makeText(MainActivity.getInstant(), "分享成功", Toast.LENGTH_SHORT).show();
		}
		break;
		case SHARE_CANCEL: {
			Log.i("", "arg1 = " + "取消分享");
			Toast.makeText(MainActivity.getInstant(), "取消分享", Toast.LENGTH_SHORT).show();
		}
		break;
		case SHARE_ERROR: {
			Log.i("", "arg1 = " + "分享错误");
			Toast.makeText(MainActivity.getInstant(), "分享错误", Toast.LENGTH_SHORT).show();
		}
		break;
	}
		return false;
	}

}
